package com.example.myapplication.tack_29;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_29.data.DetailData;
import com.example.myapplication.tack_29.data.PostData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DetailActivity extends AppCompatActivity {
    private ImageView exit;
    private Intent intent;
    private String id;
    private TextView professionName;
    private TextView obligation;
    private TextView address;
    private TextView salary;
    private TextView contacts;
    private TextView name;
    private TextView need;
    private TextView tou;
    private Gson gson;
    private DetailData data;
    private JSONObject jsonObject;
    private void init() {
        exit = findViewById(R.id.exit);
        professionName = findViewById(R.id.professionName);
        obligation = findViewById(R.id.obligation);
        address = findViewById(R.id.address);
        salary = findViewById(R.id.salary);
        contacts = findViewById(R.id.contacts);
        name = findViewById(R.id.name);
        need = findViewById(R.id.need);
        tou = findViewById(R.id.tou);
        intent = getIntent();
        id = intent.getStringExtra(Common.COMMON);
        gson = new Gson();
        jsonObject = new JSONObject();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_29_activity_detail_activity);
        init();
        initExit();
        getDetail(id);

    }

    private void getDetail(String id) {
        String url = "prod-api/api/job/post/"+id;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, DetailData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                data = (DetailData)obj;
                if(data.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(!TextUtils.isEmpty((CharSequence) data.getData().getProfessionName())){
                                professionName.setText(String.valueOf(data.getData().getProfessionName()));
                            }else{
                                professionName.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(data.getData().getObligation())){
                                obligation.setText(data.getData().getObligation());
                            }else{
                                obligation.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(data.getData().getAddress())){
                                address.setText(data.getData().getAddress());
                            }else{
                                address.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(data.getData().getSalary())){
                                salary.setText(data.getData().getSalary());
                            }else{
                                salary.setText("暂无信息");
                            }

                            if(!TextUtils.isEmpty(data.getData().getContacts())){
                                contacts.setText(data.getData().getContacts());
                            }else{
                                contacts.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(data.getData().getName())){
                                name.setText(data.getData().getName());
                            }else{
                                name.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(data.getData().getNeed())){
                                need.setText(data.getData().getNeed());
                            }else{
                                need.setText("暂无信息");
                            }
                            tou.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    try {
                                        jsonObject.put("companyId",data.getData().getCompanyId());
                                        jsonObject.put("companyName",data.getData().getCompanyName());
                                        jsonObject.put("id",data.getData().getId());
                                        jsonObject.put("money",String.valueOf(data.getData().getSalary()));
                                        jsonObject.put("postId",data.getData().getProfessionId());
                                        jsonObject.put("remark","暂无");
                                        Calendar calendar = Calendar.getInstance();
                                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                        String s1 = sdf.format(calendar.getTime());
                                        jsonObject.put("satrTime",s1);
                                        jsonObject.put("userId","1");
                                        jsonObject.put("userName","jiahuang");
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    String json = jsonObject.toString();
                                    initTou(json);
                                }
                            });

                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void initTou(String json) {
        String url = "prod-api/api/job/deliver";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DetailActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void initExit() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailActivity.this.finish();
            }
        });
    }


}